<template>
  <div>
    <div class="user_info_div">
      <div class="head_big">
        <x-img 
          :src="userPo.headImg"
          :default-src="userPo.defaultheadImg"
          :height="80"
        />
      </div>
      <div class="user_name">{{userPo.userName}}</div>
      <div class="user_detail">{{userPo.schoolName}} - {{userPo.departmentName}} - {{userPo.titleName}}</div>
    </div>

      <group label-width="4.5em" class="info_div" label-margin-right="2em" label-align="right">
      <cell title="个人信息" value="修改"  @click.native="toPassword" is-link></cell>
    </group>

    
     <div class="qiehuan_div" v-if="isShowChange">
             <x-button type="success" @click.native="doStuRole" class="dskt_btn">切换到学生角色</x-button>
    </div>


     <div class="buttom_div" v-if="isDebug">
              <x-button type="primary" id="tuichu_btn" @click.native="doOut" class="dskt_btn">退出登录</x-button>
        </div>

  </div>
</template>

<script>
import { XImg, Group, Cell } from "vux";
import dsktConstant from "../../config/constant";
import userService from '../../shared/service/userService'
import weixinService from "../../shared/service/weixinService";
const wx = require('weixin-js-sdk')
export default {
  components: {
    XImg,
    Group,
    Cell
  },
  data() {
    var data={
       userPo :  this.$cookie.getLoginUserData(),
    }
    data.realStuUserFileId =  this.$cookie.get( this.$cookie.key.openOrderStuFileId);
    data.realStuUserId =  this.$cookie.get( this.$cookie.key.openOrderStuUserId);
    data.isDebug = dsktConstant.debug;
    if(data.userPo.headImg && data.userPo.headImg !='undefined'){
      data.userPo.defaultheadImg=  data.userPo.headImg;
    }else{
        data.userPo.defaultheadImg='./static/img/avatar_default.png';
    }
    data.isShowChange=false;
    var loginUserType = this.$cookie.get(this.$cookie.key.LoginUserType);
    if (loginUserType == dsktConstant.STUDENT) {
      data.isShowChange=true;
    }

    return data;


  },
  mounted() {
     weixinService.initWx(wx);
    //如果不一样 把它换成教师的
    if(this.$cookie.get(this.$cookie.key.NOWTYPE)){
      if( 'TEACHER' !=  this.$cookie.get(this.$cookie.key.NOWTYPE) ){
        var vm =this;
        this.$vux.alert.show({
          title: '提示',
          content: '角色错误',
          onHide () {
            vm.$vux.loading.show();
            var staffNo =  vm.$cookie.get(vm.$cookie.key.StaffNo);
            userService.getTeaByNo(staffNo).then((res)=>{
              vm.loginOk(res.data);
              vm.$cookie.set(vm.$cookie.key.NOWTYPE,'TEACHER');
              vm.$vux.loading.hide();
              vm.$vux.toast.text("已切换回教师");
              location.reload();
            });
          }
        });
        
      }
    }
  },
  methods: {
    //退出登录
    toPassword(){
      this.$router.push({ name: "teacher_info", params: { id: "1" } });
    },
    //切换到学生
    doStuRole(){
      userService.getStuById( this.realStuUserId).then((res)=>{
        this.loginOk(res.data);
        this.$cookie.set(this.$cookie.key.NOWTYPE,'STUDENT');
        this.$router.push({ name: "stu_center" });
      });
    },
    //copy 登录的
    loginOk(data) {
      var user = data;
      this.$cookie.set(this.$cookie.key.userId, user.id);
      if (user.staffFile) {
        var staffFile = user.staffFile;
        this.$cookie.setLoginUserData(staffFile);
      }else if (user.studentFile) {
        var studentFile = user.studentFile;
        this.$cookie.setLoginUserData(studentFile,studentFile.id);
      }
    },
    //退出登录
    doOut(){
      const _this = this;
      this.$vux.confirm.show({
        title: '提示',
        content: '确定退出登录?',
        onCancel () {
        },
        onConfirm () {
         _this.$router.replace({ name: "login", params: { schoolNo: _this.$cookie.get(_this.$cookie.key.schoolNo) } 
          , query: {  viewType:"noWx"  } });
        }
      });
    }
  }
};
</script>

<style scoped>
@import "../../assets/scss/home.scss";
</style>

<style scoped>
.user_info_div {
  text-align: center;
    margin: 50px 20px 10px 20px;
    background:white;
    padding-bottom:20px;
      border-radius: 5px;
}
.user_info_div .user_name{
  font-size:14px;
  color:#333;
}
.user_info_div .user_detail{
  font-size:14px;
  color:#868686;
}
.user_info_div .head_big img {
  border-radius: 5px;
      position: relative;
    top: -20px;
}
  .qiehuan_div{
     padding: 15px 20%;
     padding-top:40px;
  }
 .buttom_div{
    width: 100%;
    height: 45px;
    position: absolute;
    z-index: 500;
    bottom: 90px;
    padding: 15px 20%;
    box-sizing: border-box;
  }
  #tuichu_btn{
      background:#92C5E6 !important;
  }
  .info_div{
      border-radius: 5px;
    margin: 10px 20px;
  }
  .info_div .vux-label{
     font-size:14px;
  }
  
</style>
